package com.karry.web;

import com.karry.pojo.Blog;
import com.karry.pojo.User;
import com.karry.service.BlogService;
import com.karry.service.impl.BlogServiceImpl;
import com.karry.util.JsonUtil;
import com.karry.vo.ResultInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 *  博客信息统计类
 *  主要实现依赖前端以及第三方技术，不做介绍
 * */
@WebServlet("/report")
public class ReportServlet extends HttpServlet {

    private final BlogService bs = new BlogServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置首页导航栏的高亮值
        request.setAttribute("menu_page", "report");

        // 得到用户行为
        String actionName = request.getParameter("actionName");

        System.out.println("report_actionName:" + actionName);

        switch (actionName){
            case "info":
                reportInfo(request, response);
                break;
            case "month":
                queryNoteCountByMonth(request, response);
                break;
            case "location":
                queryNoteLonAndLat(request, response);
                break;
            case "user":
                queryNoteCountByUser(request, response);
                break;
            default:break;
        }

    }

    private void queryNoteCountByUser(HttpServletRequest request, HttpServletResponse response) {
        // 调用Service层的查询方法，返回ResultInfo对象
        ResultInfo<Map<String, Object>> resultInfo = bs.searchBlogCountByUser();

        System.out.println(resultInfo.toString());
        // 将ResultInfo对象转换成JSON格式的字符串，响应给ajax的回调函数
        JsonUtil.toJson(response, resultInfo);
    }


    /**
     * 查询用户发布云记时的坐标
     * @param request
     * @param response
     */
    private void queryNoteLonAndLat(HttpServletRequest request, HttpServletResponse response) {
        // 从Session作用域中获取用户对象
        User user = (User) request.getSession().getAttribute("user");
        // 调用Service层的查询方法，返回ResultInfo对象
        ResultInfo<List<Blog>> resultInfo = bs.searchBlogListByUserID(user.getId());
        // 将ResultInfo对象转换成JSON格式的字符串，响应给AJAX的回调函数
        JsonUtil.toJson(response, resultInfo);

    }

    /**
     * 通过月份查询对应的云记数量
     * @param request
     * @param response
     */
    private void queryNoteCountByMonth(HttpServletRequest request, HttpServletResponse response) {
        // 从Session作用域中获取用户对象
        User user = (User) request.getSession().getAttribute("user");
        // 调用Service层的查询方法，返回ResultInfo对象
        ResultInfo<Map<String, Object>> resultInfo = bs.searchBlogCountByMonth(user.getId());
        // 将ResultInfo对象转换成JSON格式的字符串，响应给ajax的回调函数
        JsonUtil.toJson(response, resultInfo);
    }

    /**
     * 进入报表页面
     * @param request
     * @param response
     */
    private void reportInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置首页动态包含的页面值
        request.setAttribute("changePage","report/info.jsp");
        //request.setAttribute("changePage","test/pinglun.jsp");
        // 请求转发跳转到index.jsp
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }
}
